package com.example.zcq.rxjava2retrofitmvp.base;

import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.example.zcq.rxjava2retrofitmvp.constant.Constant;

/**
 * @author zcq
 * @package com.example.zcq.rxjava2retrofitmvp.base
 * @fileName Rxjava2RetrofitMVP
 * @description: 基类activity
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/9/25  15:55
 * <p>
 * 扩展内容：动态权限框架使用-EasyPermissions   https://github.com/googlesamples/easypermissions
 * 参考博客：
 * <p>
 * star 5K多，而且一直在更新，不错
 * easypermissions
 * <p>
 * 一行代码搞定漂亮的Android6.0权限申请界面
 * HiPermission
 * <p>
 * AndPermission
 * <p>
 * 这个关注度比较高：star 4K多 也可作为备用
 * PermissionsDispatcher
 * <p>
 * ---------------------
 */
public class BaseActivity extends AppCompatActivity {
    /**
     * 申请指定的权限.
     */
    public void requestPermission(int code, String... permissions) {

        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(permissions, code);
        }
    }

    /**
     * 判断是否有指定的权限
     */
    public boolean hasPermission(String... permissions) {

        for (String permisson : permissions) {
            if (ContextCompat.checkSelfPermission(this, permisson)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case Constant.HARDWEAR_CAMERA_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doOpenCamera();
                }
                break;
            case Constant.WRITE_READ_EXTERNAL_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doWriteSDCard();
                }
                break;
            case Constant.WRITE_READ_EXTERNAL_CAMERA_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    douyou();
                }
                break;
            case Constant.READ_CONTACTS_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    seeContacts();
                }
                break;
        }
    }

    public void seeContacts() {

    }

    public void douyou() {

    }

    public void doOpenCamera() {

    }

    //读写sd卡业务逻辑，有具体的子类实现
    public void doWriteSDCard() {

    }
}
